Solved

PHP MySQL query problem

Posted on 2014-03-10
6
599 Views
Last Modified: 2014-03-10
This works:
 $result = mysqli_query($con,"SELECT * FROM `Persons` WHERE PhoneNo='5555555555'");
 while ($row = mysqli_fetch_row($result)) 
   {
      echo $row[0] . " " . $row[1];
   }

Open in new window

(Phone number changed for privacy.)

This doesn't work:
$number = '5555555555';
$result = mysqli_query($MySQLcon,"SELECT * FROM 'Persons' WHERE PhoneNo='" . $number . "'");

Open in new window

What am I doing wrong? I also tried removing the single quotes around $number.

BTW: Is there a better way to retrieve the fields if I am selecting only one record than this?
while ($row = mysqli_fetch_row($result)) 
   {
	echo $row[0] . " " . $row[1];
   }

Open in new window

0
Comment
Question by:thenelson
6 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 350 total points
ID: 39918444
It's because your table in the second example is enclosed in single quotes, not backticks:

...FROM 'Persons'

Try changing it to:

FROM `Persons`
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39918446
You have straight quotes on the second example. That's all the difference I see.
Try
$number = '5555555555';
$result = mysqli_query($MySQLcon,"SELECT * FROM `Persons` WHERE `PhoneNo`='" . $number . "'");

Open in new window


HTH,
Dan
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 150 total points
ID: 39918448
$row = $result->fetch_assoc()
Then just access each column as normal.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39918551
The MySQLi functions return a value.  You can test this value to determine if the query worked or failed.  If it failed you can print out the MySQLi_error() data.

In the first case, it says this:
mysqli_query($con...

Open in new window

In the second case it says this:
mysqli_query($MySQLcon...

Open in new window

So maybe the connection name is incorrect?

Here is how I might write it so that I could get diagnostic information
error_reporting(E_ALL);
$number = '5555555555';
$query  = "SELECT * FROM Persons WHERE PhoneNo='$number' LIMIT 1";
$result = mysqli_query($con, $query);
if (!$result)
{
    echo "FAIL: $query <br>";
    trigger_error(mysqli_error(), E_USER_ERROR);
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39918560
a better way to retrieve the fields if I am selecting only one record
Yes, I would just do this, omitting the iterator:
if (!$result)
{
    echo "FAIL: $query <br>";
    trigger_error(mysqli_error(), E_USER_ERROR);
}
$row = mysqli_fetch_object($result);
print_r($row);

Open in new window

The advantage of the fetch_object method is simpler punctuation, fewer fiddly quotation marks, ease of integration into HTML templates that are created with HEREDOC, etc.
0
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39918828
Yep, that was it. I guess being new to php and MySQL and my old eyes, I didn't see the difference between single quotes and back tics - thanks.

I guess I should have asked about $row = $result->fetch_assoc() as a separate question instead of a BTW but here is some points for that. Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question