le007 Posted August 26, 2008 Share Posted August 26, 2008 <?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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/ Share on other sites More sharing options...
Cosizzle Posted August 26, 2008 Share Posted August 26, 2008 "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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626257 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 thanks buddy, that got it - unfortunately no data is coming up and I have some in there hahah Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626260 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 <?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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626266 Share on other sites More sharing options...
Cosizzle Posted August 26, 2008 Share Posted August 26, 2008 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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626272 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626279 Share on other sites More sharing options...
Cosizzle Posted August 26, 2008 Share Posted August 26, 2008 Perhaps its your connection try this up top $conn = mysql_connect("localhost", "root", "") OR die ('Could not connect to mySQL: ' . mysql_error());; mysql_select_db("table1") OR die ('Could not select a Database: ' . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626286 Share on other sites More sharing options...
runnerjp Posted August 26, 2008 Share Posted August 26, 2008 ok so your trying to pulll the data from the db <?php $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 } ?> Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626288 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 No, it seems to be connecting alright, no error message - I'll try and insert something to the field and see if that works. Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626290 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 I just tried mysql_query("INSERT INTO one (keyword, link, description) VALUES ('Peter', 'Griffin', '35')"); and it did insert the data..... Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626296 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626303 Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 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 https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626323 Share on other sites More sharing options...
le007 Posted August 26, 2008 Author Share Posted August 26, 2008 Ok yep - that worked a charm. Now on to challenge two - getting keywords from a posted form and matching them to link and desc Fun and games ahead :L Link to comment https://forums.phpfreaks.com/topic/121446-db-connection-issue/#findComment-626335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.