Jump to content

Db connection issue?


le007

Recommended Posts

<?php
$conn = mysql_connect("localhost", "root", "") or die  

mysql_select_db("one", $conn);

$result = mysql_query("SELECT * FROM t1");

echo $result;

?>

 

I'm using WAMP, I tried putting a password as password - still nothing, the db is called one, the table is called t1

 

Parse error: parse error, unexpected T_STRING in line 4

Link to comment
Share on other sites

 

"Parse error: parse error, unexpected T_STRING in line 4" tends to mean a missing ';' or ',' I saw one was missing on line 1

<?php
$conn = mysql_connect("localhost", "root", "") or die;

mysql_select_db("one", $conn);

$result = mysql_query("SELECT * FROM t1");

echo $result;

?>

Link to comment
Share on other sites

<?php
$conn = mysql_connect("localhost", "root", "") or die;

mysql_select_db("datab", $conn);

$result = mysql_query("SELECT * FROM one");

echo $result;
?>

I have data in three fields as follows:

date

 

time

 

day

 

All is written is this "Resource id #3" ?????? Any ideas? Thanks

 

Link to comment
Share on other sites

It's the way your date is being passed. Your really not out putting anything but the query.  You need to define which each table name is then output them.

 

try this:

 

$date = $_POST['date'];
$time = $_POST['time'];
$day = $_POST['day'];

$query = "SELECT * FROM one";
$result = mysql_query($query) or trigger_error("Query: $query <br/>mysql Error: " . mysql_error());

echo "Today's date is $date";
echo "The time is $time";
echo "The day is $day";

Link to comment
Share on other sites

thanks for the help, my fields are actually keyword, link and description - i tried the following and didnt get any data back at all, just the keyword is link is description is?

 

<?php
$conn = mysql_connect("localhost", "root", "") or die;

mysql_select_db("table1", $conn);


$date = $_POST['keyword'];
$time = $_POST['link'];
$day = $_POST['description'];


$query = "SELECT * FROM one";
$result = mysql_query($query) or trigger_error("Query: $query <br/>mysql Error: " . 

mysql_error());

echo "keyword is $date";
echo "link is $time";
echo "description is $day";

?>

Link to comment
Share on other sites

thanks for the help fellas, i just inserted data no problem and then I did the

    $sql = "SELECT * FROM one";

    $data = mysql_query( $sql ) or die( "Could not get threads" ); 

    while ( $data2 = mysql_fetch_array( $data ) ) 
{
$link = $data2[link];
// fill in other 2
}

 

it worked, I got one piece of data back, where do I put the others to get the rest of it?

I just thought if I put select * it would show everthing in the table, I guess not......

Link to comment
Share on other sites

Okay.  Let me just explain everything that happened in this thread and why it happened:

 

It echoed "Resource id #3"

This is actually a relatively common error actually (as far as I've seen).  The mysql_query() function actually just returns a MySQL result resource, and not the direct data.  A resource in PHP is a special type that's created by some functions that actually represents something else, it can be anything.  A file resource from fopen() points to a file, etc.  Echoing a resource just shows you PHP's internal resource number for that resource.  Resources have associated functions that actually handle them properly.  In MySQL's case, it's usually the mysql_fetch_*() functions.  It returns one row from the result set, and then advances the internal pointer by one.  It's most commonly used in a loop, like runnerjp showed in order to get ALL the results from a query.

 

It showed only one result

Look carefully at your loop.  First of all, the array syntax is kind of faulty, as the ' ' were left off of the array key, which can affect performance.  (If you want to know why, I'll be glad to explain).  That doesn't actually throw an error (yet), but you never know.  Secondly, you just overwrite $link with the latest row's link data, instead of using it in the loop.  For example: (example data and code)

mysql> SELECT * FROM some_table;

+---------+

| test    |

+---------+

| testing |

| test1   |

+---------+

<?php
//assume we're connected
$sql = mysql_query("SELECT * FROM some_table") OR die(mysql_error());
while ($row = mysql_fetch_assoc($sql)) {
     echo $row['test'] . "<br />";
}

 

See how the data is used INSIDE the loop rather than assigned to a variable, overwriting the previous row?  This is how it should be done.  Try using your data inside the loop.

 

Link to comment
Share on other sites

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.