PDA

View Full Version : php script to extract thread titles


Cory Megitt
10-23-2011, 11:59 AM
I'm starting to program a php script to extract thread titles.
I am new to PHP but I'm learning ...


<?php
$connect=mysql_connect('localhost', 'root', 'xxxxx') or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("vb") or die(mysql_error());
echo "Connected to Database";
$sql="SELECT 'title' FROM 'thread' WHERE 'dateline' BETWEEN 1315958401 AND 1318723199";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "$row <br />";
}
?>


The output of this is:


Connected to MySQL
Connected to Database
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/mysql.php on line 8

kh99
10-23-2011, 12:32 PM
I think the problem is the quotes you have around the column and table names. The quote character should be a backtick (`) and it looks like you have single quotes. (You don't really have to quote column and table names unless they have special characters).

Also, if you look at the page for mysql_query (http://us2.php.net/manual/en/function.mysql-query.php) under the Return Values section, you'll see that it can return FALSE if there's an error, so that's why your error message says that mysql_fetch_array() was expecting an array.

Cory Megitt
10-23-2011, 12:45 PM
I think the problem is the quotes you have around the column and table names. The quote character should be a backtick (`) and it looks like you have single quotes. (You don't really have to quote column and table names unless they have special characters).

Also, if you look at the page for mysql_query (http://us2.php.net/manual/en/function.mysql-query.php) under the Return Values section, you'll see that it can return FALSE if there's an error, so that's why your error message says that mysql_fetch_array() was expecting an array.

I've adjusted the line to read:

$sql="SELECT `title` FROM `thread` WHERE `dateline` BETWEEN 1315958401 AND 1318723199";


I've adjusted the single quotes to backticks. I now have an output that looks like this:


Connected to MySQL
Connected to DatabaseArray
Array
Array
Array
Array
Array
Array
Array
Array
Array
......... (it keeps on going for a little while)

kh99
10-23-2011, 01:03 PM
That probably means it's working. The return from mysql_fetch_array() will be an array even if you are only selecting one column. So if you change your echo to $row['title'] you should see the titles.

setishock
10-23-2011, 01:11 PM
Pardon my butting in but my curiosity is killing me. What would you use that for?

Cory Megitt
10-23-2011, 01:15 PM
kh ... I get this now:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Applications/XAMPP/xamppfiles/htdocs/mysql.php on line 10

setishock - The reason for this is to generate a report of thread titles from the 15th of each month to the 14th of each month.
Based on the data I have, it will populate an excel spreadsheet and make my monthly reporting for the manager a lot less time consuming.

It's only the start of what I want to have the script do.

kh99
10-23-2011, 01:17 PM
kh ... I get this now:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Applications/XAMPP/xamppfiles/htdocs/mysql.php on line 10



That usually means that quotes are mismatched or something.

Cory Megitt
10-23-2011, 01:27 PM
<?php
$connect=mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("vb") or die(mysql_error());
echo "Connected to Database";
$sql="SELECT `title` FROM `thread` WHERE `dateline` BETWEEN 1315958401 AND 1318723199";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "$row['title']";
}
?>


That's what it looks like -- and it appears to have proper quotes.

kh99
10-23-2011, 01:44 PM
Oh right. Sorry, change the echo line to

echo "$row[title]";

Cory Megitt
10-23-2011, 02:14 PM
<?php
$connect=mysql_connect('localhost', 'root', 'glide75') or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("vb") or die(mysql_error());
echo "Connected to Database<br />";
$sql="SELECT `title` FROM `thread` WHERE `dateline` BETWEEN 1315958401 AND 1318723199";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "$row[title]<br />";
}
?>


This is the final and working script.

Thanks for the help kh99.
It worked.
Just need to figure out how to calculate the from date and to date properly as needed.

Judge Dredd X
10-25-2011, 03:10 AM
I just wanted to let you know that this part:
$connect=mysql_connect('localhost', 'root', 'glide75') or die(mysql_error());
echo "Connected to MySQL<br />";

Would output "Connected to MySQL" in addition to the mysql error if there is one.

So basically, if the connection fails, it will display the error and Connected to MySQL.

I am not very good at PHP, but I believe you could write an if statement for this.

if ($connect) {
echo "Connected to MySQL<br />";
}
else {
die();
}

kh99
10-25-2011, 03:16 AM
I just wanted to let you know that this part:
$connect=mysql_connect('localhost', 'root', 'glide75') or die(mysql_error());
echo "Connected to MySQL<br />";

Would output "Connected to MySQL" in addition to the mysql error if there is one.


That's a good thought, but in the case of a connection error the echo line wouldn't execute because die() ends the script.

Judge Dredd X
10-25-2011, 03:24 AM
That's a good thought, but in the case of a connection error the echo line wouldn't execute because die() ends the script.

Ah!

So maybe instead of die(mysql_error())

He could use or echo("mysql_error()")?

Please don't bash me if the above doesn't work, I'm not experienced and I'm also not sure if "or" works with echo.

kkinsey
10-28-2011, 05:40 PM
not sure if "or" works with echoIt doesn't.

However, you can concatenate strings, vars, and function calls inside the die() statement://let's be verbose...
die("Connected to SQL, but this program sucks because ".$somereasonhere." and MySQL said: ".mysql_error());HTH,