Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP MySQL query problem

Posted on 2014-03-10
6
Medium Priority
?
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1400 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 600 total points
ID: 39918448
$row = $result->fetch_assoc()
Then just access each column as normal.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 111

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 111

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

618 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