Solved

PHP MySQL query problem

Posted on 2014-03-10
6
446 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 34

Accepted Solution

by:
gr8gonzo earned 350 total points
Comment Utility
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 34

Expert Comment

by:Dan Craciun
Comment Utility
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
Comment Utility
$row = $result->fetch_assoc()
Then just access each column as normal.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now