PHP and SQL

  • techwiz
  • Beginner
  • Beginner
  • User avatar
  • Posts: 46

Post 3+ Months Ago

Table persons:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


Imagine I have the above table in a mysql database...

I would like to get the "P_Id" of the person who lives at "Storgt 20" from a mysql query

I thought that the following would work:

MYSQL Code: [ Select ]
SELECT P_Id FROM `persons` WHERE Address='Storgt 20'


Unless some of my php isn't working, this doesn't work

any suggestions?
thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

Try this

MYSQL Code: [ Select ]
mysql_query("SELECT P_Id FROM persons WHERE Address='Storgt 20' ");


Looking at it again, I can't really remember if the single quotes are needed around 'persons'. but worth a shot.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

natas, those are backticks, and they're only really required in cases when a table name uses reserved words. :D

techwiz, my first guess is that it has something to do with your PHP / MySQL connection character set. The query looks solid.

It's been so long since I've had to deal with a character set mis-synchronization, I've forgotten where to start. :scratchhead:

Another thought, is that perhaps your data was copied from a source which had spaces on the end of each line. A quick way to test for that is with the following query.

SQL Code: [ Select ]
SELECT COUNT(P_Id) FROM persons WHERE Address LIKE '% '
  • sandy_j_sam
  • Newbie
  • Newbie
  • sandy_j_sam
  • Posts: 9

Post 3+ Months Ago

Hi, for achiving that you first create connection, databse,then that table later You Execute Query, now I Introduce whole task here seee them

<?php
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('db_name',$conn);
$sql = mysql_qyery("SELECT P_Id FROM person where Address ='Storgt 20' "),$conn);
?>

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 136 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.