Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.