1

How do I get data from a database using php and show it?

The database table has columns, labeled as ID & Number. ID is unique & fixed whereas Number is just a non-unique number. If someone visits http://example.com/show.php?ID=32, and show.php should fetch the Number & display "Your number is XXX”

Please provide the code-samples.

7
  • 1
    Looks like the database has one table with two fields. Please clarify your question. Also, do you know how to connect to the database? and what database system is it (MySQL, PostgreSQL, etc)? Commented May 23, 2011 at 8:27
  • 2
    @Tash, He does specify mysql in the tags. Commented May 23, 2011 at 8:42
  • RobertPitt seems to have the right answer. I cannot vote here without registration, so I registerd to vote. Commented May 23, 2011 at 8:59
  • I am allowed to ask additional questions or do I need to open a new question? Commented May 23, 2011 at 9:13
  • each question requires you to create a new question, but remember to search before you ask as the chances are that it's already been asked Commented May 23, 2011 at 9:21

4 Answers 4

1
<?php
//Connect to DB
$db = mysql_connect("localhst","user","pass") or die("Database Error");
mysql_select_db("db_name",$db);

//Get ID from request
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

//Check id is valid
if($id > 0)
{
    //Query the DB
    $resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
    if($resource === false)
    {
        die("Database Error");
    }

    if(mysql_num_rows($resource) == 0)
    {
        die("No User Exists");
    }

    $user = mysql_fetch_assoc($resource);

    echo "Hello User, your number is" . $user['number'];
}

This is very basic but should see you through.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is it! I have to register to give you a point. Strangely now I have 3 different users using the same email. I think it is a stackoverflow bug or I am being stupid.
1

First get the id of the user(it may given while visiting or based on the details given while visiting)

Then write select query to the table which contains 'number' field.like

SELECT number FROM table1 WHERE table1.ID=IDFromtheuser;

Comments

0

try

SELECT number from numberTable nt
JOIN idTable it ON  it.ID = nt.ID
WHERE it.ID = `your given id`

as i think your both tables are referenced with id

Comments

0
<?php
$host="localhost"; 
$username=""; 
$password=""; 
$db_name="multiple_del"; 
$tbl_name="test_mysql"; 

// Connect to server and select databse.
mysql_connect("$host", "root", "")or die("cannot connect");
mysql_select_db("multiple_del")or die("cannot select DB");

$sql="SELECT * FROM test_mysql";


$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

</table>
</form>
</td>
</tr>
</table>



its my code but its not working and error are show in this code:-

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

please help me.

1 Comment

Just add a sentence to explain. It will improve your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.