The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Select data from mysql
Discuss Select data from mysql in the PHP Development forum on Dev Shed. Select data from mysql PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 24th, 2012, 12:03 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 34
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
|
|
|
Select data from mysql
Hi,
1. I have inserted a row in users table as
insert into users
values ( 1 , 'shams' , 'pw1' , 'hh@hero.com' )
2. on my site, i entered user name as 'shams' and password as 'pw1' then click login.
Question : I can connect to db just fine but its not printing the result(please see last line of code) after i enter shams and pw1 as username and password. Please help.
<?php
session_start();
//This displays your login form
echo "<form action='?act=login' method='post'>"
."Username: <input type='text' name='username' size='30'><br>"
."Password: <input type='password' name='password' size='30'><br>"
."<input type='submit' value='Login'>"
."</form>";
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
//echo $username;
$con = mysql_connect("host","db","7hhg3@");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "con";
}
$result = mysql_query("SELECT id FROM users");
echo $result;
?>
|

December 24th, 2012, 12:30 PM
|
|
|
1) Please enclose your code in [ PHP ] tags (see the sticky at the top of this forum).
2) Do not use the deprecated mysql extensions. Switch to PDO.
3) Like PDO the query returns a result object not the row data itself. You need to do a 'fetch' or 'fetchall' on that object.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

December 24th, 2012, 01:53 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 34
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
|
|
|
Hi,
I am not sure how to use the PHP tag. I could not find the instructions. Please provide.
I got some improvement. Below code does not give me any error. but it does not print anything inside the while loop even though i have data in the table.
Please help.
<?php
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = "SELECT * FROM users";
$result = mysql_query($query);
while($row = @mysql_fetch_array($result))
{
echo $row['username'];
echo "rr";
echo "<br />";
}
mysql_close($con);
?>
|

December 24th, 2012, 02:09 PM
|
|
|
You highlight your code then click the PHP icon.
I rarely do this but in the spirit of Christmas, here is what you need:
PHP Code:
<?php
try {
$conn=new->PDO("mysql:host=$host;dbname=$db",$user,$pass);
}
catch (PDOException $e) {
die("Connection failure: ".$e->getMessage());
}
$query = "SELECT * FROM users";
$result = $conn->query($query);
foreach($result as $row)
{
echo $row['username'];
echo "rr";
echo "<br />";
}
?>
Of course you will need to set $host, $db, $user and $pass as appropriate.
|

December 24th, 2012, 02:29 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 34
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
|
|
Merry Christmas!!
Many thanks for your time but my problem is not solved yet tho. I am getting error message:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting T_STRING or T_VARIABLE or '$' in /home/content/ .......................... on line 7
PHP Code:
<?php
$host = "......" ; // i copied everything under hostname in here
$db = "......." ; // name of the database
$user = "......" ; //db name and user name same
$pass = "......"; //pass goes here
try {
[B] $conn=new->PDO("mysql:host=$host;dbname=$db",$user,$pass); [/B] // line 7
}
catch (PDOException $e) {
die("Connection failure: ".$e->getMessage());
}
$query = "SELECT * FROM users";
$result = $conn->query($query);
foreach($result as $row)
{
echo $row['username'];
echo "rr";
echo "<br />";
}
?>
|

December 24th, 2012, 02:41 PM
|
|
|
Oops. Sorry about that.
PHP Code:
$conn=new PDO("mysql:host=$host;dbname=$db",$user,$pass);
|

December 24th, 2012, 03:21 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 34
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
|
|
It was very nice of you. It worked just fine.
If you dont mind, could you please tell me why my code does not print out anything in while loop. I dont also get any error.
PHP Code:
<?php
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = "SELECT * FROM users";
$result = mysql_query($query);
while($row = @mysql_fetch_array($result))
{
echo $row['username'];
echo "rr";
echo "<br />";
}
mysql_close($con);
?>
|

December 24th, 2012, 03:32 PM
|
|
|
|
That is not the code I gave you and I don't use obsolete extensions.
|

December 24th, 2012, 03:48 PM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Location: Oxford, United Kingdom
Posts: 40

Time spent in forums: 1 Day 2 h 18 m 23 sec
Reputation Power: 1
|
|
The code you have is not printing anything because you're suppressing errors and you're treating $row as an assoc when it's not.
Although you shouldn't be using this code at all, here is the working version:
PHP Code:
<?php
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = "SELECT * FROM users";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo $row['username'];
echo "rr";
echo "<br />";
}
mysql_close($con);
?>
I strongly recommend you to use PDO, as the nice chap gw1500se has demonstrated for you.
If you find it hard to understand PDO, you haven't grasped the basics. Do some research on Object-Oriented Programming. There's so much info out there. You just need to take the time to find it.
Bad habits are hard to forget. You've been warned. If you're going to be stupid enough to deliberately pick up bad habits, without good reason, you may as well give up now.
|

December 24th, 2012, 04:05 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 34
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
|
|
|
Many thanks for your help!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|