Jump to content

[SOLVED] how to create a Mysql query in a PHP page?


samoi

Recommended Posts

Hello guys, how are you?

 

again, I've been working in a one-line code to get it work, but I think I have some mistake!

 

 

for example, let's say I have this code:

<?

$con = mysql_connect("localhost","root","password") or die("Error connecting!");

$db = mysql_select_db("this_db") or die("Couldn't find the DB");

$samoi = $db->query_first("SELECT * FROM users WHERE id <= '10'"); // this is going to get info from id 1 to 10, right?

echo $samoi['username'];
echo "<br>";
echo $samoi['password'];
echo "<br>";
echo $samoi['email'];

?>

 

I tried it, but it only brings

Try this:

 

<?php

$con = mysql_connect("localhost","root","password") or die("Error connecting!");

$db = mysql_select_db("this_db") or die("Couldn't find the DB");

$sql = "SELECT * FROM users WHERE id <= '10'"; // this is going to get info from id 1 to 10, right?
$result = mysql_query($sql) or die(mysql_error());

echo "username password email <br />";
while ($samoi = mysql_fetch_assoc($result)) {
   echo $samoi['username'];   
   echo " ";
   echo $samoi['password'];
   echo " ";
   echo $samoi['email'];
   echo "<br />";
}
?>

ok what are you trying to get a list of users and other information or just 1 user and info???

for more then 1 thing i would do this

$con = mysql_connect("localhost","root","password") or die("Error connecting!");

$db = mysql_select_db("this_db") or die("Couldn't find the DB");

$result = $db->query_first("SELECT * FROM users <=10"); // this is going to get info from id 1 to 10, right?

while(list($username, $password, $email) = mysql_fetch_array($result)) {
echo $username;
echo "<br>";
echo $password;
echo "<br>";
echo $email;
}

this will loop thro and list all you variables 1 thro 10

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.