PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 24th, 2012, 12:03 PM
phpstudent phpstudent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 34 phpstudent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;


?>

Reply With Quote
  #2  
Old December 24th, 2012, 12:30 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,877 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 16 h 44 m 33 sec
Reputation Power: 581
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.

Reply With Quote
  #3  
Old December 24th, 2012, 01:53 PM
phpstudent phpstudent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 34 phpstudent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
?>

Reply With Quote
  #4  
Old December 24th, 2012, 02:09 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,877 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 16 h 44 m 33 sec
Reputation Power: 581
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.

Reply With Quote
  #5  
Old December 24th, 2012, 02:29 PM
phpstudent phpstudent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 34 phpstudent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 />"

?>

Reply With Quote
  #6  
Old December 24th, 2012, 02:41 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,877 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 16 h 44 m 33 sec
Reputation Power: 581
Oops. Sorry about that.
PHP Code:
 $conn=new PDO("mysql:host=$host;dbname=$db",$user,$pass); 

Reply With Quote
  #7  
Old December 24th, 2012, 03:21 PM
phpstudent phpstudent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 34 phpstudent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
?>

Reply With Quote
  #8  
Old December 24th, 2012, 03:32 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,877 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 16 h 44 m 33 sec
Reputation Power: 581
That is not the code I gave you and I don't use obsolete extensions.

Reply With Quote
  #9  
Old December 24th, 2012, 03:48 PM
navnav's Avatar
navnav navnav is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Oxford, United Kingdom
Posts: 40 navnav User rank is Private First Class (20 - 50 Reputation Level)navnav User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 2 h 18 m 23 sec
Reputation Power: 1
Send a message via Skype to navnav
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.

Reply With Quote
  #10  
Old December 24th, 2012, 04:05 PM
phpstudent phpstudent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 34 phpstudent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 42 m 6 sec
Reputation Power: 1
Many thanks for your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Select data from mysql

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap