Converting from ASP to PHP
Hi.
I have been working with ASP for a while now, but it is time for me to start to learn PHP.
In ASP you have recordsets, and then you can use this code :
<?php
=(Recordset1.Fields.Item("contact_firstname").Value)
?>
My question is, What is the PHP way of doing this? I can connect to MySQL, and I can retreive information. How ever, I want to be able to have something like the ASP recordset at the top of the pages, and then be able to insert something like the ASP code above to show the results thorughout the page. There could be a number of these round the page.
I hope this makes sense.
Thank you in advance for your help.
teammatt3 posted this at 19:09 — 17th April 2008.
He has: 2,102 posts
Joined: Sep 2003
So you're getting one row of data, and you want to display it in different places around the code/page. I don't think there's really a built in OO way to do that, here's what I would do:
<?php
$result = mysql_query(\"SELECT * FROM Contact WHERE ContactID = 123\");
$recordset1 = mysql_fetch_assoc($result);
...
echo $recordset1['FirstName'];
...
echo $recordset1['LastName'];
?>
dhotchin posted this at 19:51 — 17th April 2008.
He has: 183 posts
Joined: Nov 2003
Hi. Thanks for your reply.
Yes that is what I would like to do. I have tried what you have suggested before, but I have HTML content as well and I can not place the html content inside the ?> because it makes the PHP code invalid. so I can't seem to make it work. (Only when it is installed like yours is when it will work). The full PHP script inside a table.
I would like something like this:
<?php
$result = mysql_query("SELECT * FROM Contact WHERE ContactID = 123");
$recordset1 = mysql_fetch_assoc($result);
?>
Then the html content
echo $recordset1['LastName'];
And anywhere inside the html content (in a table) will then show echo $recordset1['LastName'];
Does this make sense?
Thanks.
teammatt3 posted this at 20:35 — 17th April 2008.
He has: 2,102 posts
Joined: Sep 2003
Ok, I think I understand what you want. To output PHP within HTML, you do something like this (I couldn't post it here because the formatting is screwing the code up)
In the HTML you can put PHP short tags (I think that's what they're called). The syntax is <?=$var_name?> It also supports some logic, but I don't think you care about that.
dhotchin posted this at 20:41 — 17th April 2008.
He has: 183 posts
Joined: Nov 2003
Perfect. That's the answer. Thank you very much for pointing me in the right direction!
dhotchin posted this at 10:28 — 18th April 2008.
He has: 183 posts
Joined: Nov 2003
Well, been working on this for hours now, and I still can't get a result. Wonder if anyone can help.
Here is the php code that I am using to connect to the database:
<?php
// db connect
function connect()
{
//live settings
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("admin_appl", $db);
if (!$db){
die('could not connect');
}
return $db;
}
function disconnect($db)
{
mysql_close($db);
}
// SQL for tbl_search_results
$query = "SELECT * FROM tbl_search_results WHERE confcode = 'hrc010'");
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
$recordset1 = mysql_fetch_assoc($result);
?>
And here is the code that I wish to place inside the html:
<?=$recordset1['title']?>
There may be just something simple stopping this working. But its gone way over my head.
Cheers.
teammatt3 posted this at 03:42 — 24th April 2008.
He has: 2,102 posts
Joined: Sep 2003
At first glance, I see you aren't connecting to the database. You need to run connect() (just because you define it, doesn't make it run).
<?php
lot of code...
// connect to DB
connect();
// SQL for tbl_search_results
...lots of code
?>
dhotchin posted this at 11:37 — 24th April 2008.
He has: 183 posts
Joined: Nov 2003
Hi teammatt3. Thanks for you comment. I have // db connect
function connect() at the top of the script. Is this not the same thing?
Thanks,
Darren
greg posted this at 15:06 — 24th April 2008.
He has: 1,581 posts
Joined: Nov 2005
At the top of the script you have made your own "user defined" function and called it "connect".
I.E. You have told PHP what the function called "connect" is going to do.
But as teammatt3 said, you need to actually run that function like this-
connect();
So you made a function that connects to the database and called it "connect", so before your query, run that function so it performs the connection.
I copied and pasted your code from your last post and added the required bit to run the function you made.
<?php
// db connect
function connect()
{
//live settings
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("admin_appl", $db);
if (!$db){
die('could not connect');
}
return $db;
}
//db disconnect
function disconnect($db)
{
mysql_close($db);
}
//run the function that connects to the DB
connect();
// SQL for tbl_search_results
$query = "SELECT * FROM tbl_search_results WHERE confcode = 'hrc010'");
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
$recordset1 = mysql_fetch_assoc($result);
//run the function that disconnects from the DB
//(this wont work by the way, I explain why at the end)
disconnect();
?>
Some other notes.
You dont have to close the DB connection (mysql_close) as it will automatically close it when the script is finished excecuting
You can just have the database connection parameters without a function, like so
<?php
//connect to database
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("admin_appl", $db);
// SQL for tbl_search_results
$query = "SELECT * FROM tbl_search_results WHERE confcode = 'hrc010'");
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
$recordset1 = mysql_fetch_assoc($result);
?>
What I usually do is make a seperate file and save the database connection values in that. Then whenever I have a page where I have a query, I just include that file with the database connection details in it.
In my opinion I think it's pointless to have the database connect parameters inside a function as you have it, simply because it's extra code to define it as a function then extra code to call it later in the script.
The point of creating a function is to be able to use it whenever you want, as many times as you want. I generally only make a function when I need a block of code that I will use a few times on the same page.
I might be wrong, but I dont think you quite understood user defined functions.
If I'm correct, have a read of this, it explains it very well
http://www.tizag.com/phpT/phpfunctions.php
The reason your disconnect function wont work is because it is using the variable $db to identify what connection to close. But the variable $db is defined within another function, and therefore is out of the scope of anything outside that function.
You can make it work by declaring the variables inside a function to be "Global"..
So, something else you might want to read to understand the relationship between variables inside and outside of "user defined" functions:
http://us2.php.net/variables.scope
greg posted this at 15:30 — 24th April 2008.
He has: 1,581 posts
Joined: Nov 2005
Another thing I just noticed with your script too.
you have this:
<?php
// SQL for tbl_search_results
$query = "SELECT * FROM tbl_search_results WHERE confcode = 'hrc010'");
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
$recordset1 = mysql_fetch_assoc($result);
?>
The query code ($query) has an unnecessary bracket at the end.
Also, $numresults and $result are both doing the same thing, there is no need for both.
Here is a simpler way with exactly the same result:
<?php
// SQL for tbl_search_results
$query = mysql_query("SELECT * FROM tbl_search_results WHERE confcode = 'hrc010'");
$numrows = mysql_num_rows($query);
$recordset1 = mysql_fetch_assoc($result);
?>
Notice how I've taken out the line -
$result = mysql_query($query) or die("Couldn't execute query");
You shouldnt really use "or die" as it will output errors to site users, if it's public that's potentially bad.
The mysql_num_rows is the amount of rows mysql returned from the query. If it didnt find "hrc010" from the specified table then $numrows will be zero
So what is often better is outputting a message like this
<?php
if ($numrows <=0){
echo "Nothing found";
}
?>
If nothing is found then $numrows will be 0. Sometimes it can return less than 0 so is always safe to put less than or equal to zero
mysql_num_rows() only returns a value for selecting from the table
If you are changing the table, i.e. insert, delete etc, then use mysql_affected_rows()
Logically, as delete/insert etc will or wont have 'affected' the table
dhotchin posted this at 20:56 — 24th April 2008.
He has: 183 posts
Joined: Nov 2003
Hi. Thank you ever so much for you help.
The code I have used was taken from another script that I have just modified so I can use it as I learn. Your comments have helped me a great deal. I will post my outcome when I have time to implement it.
Thanks
Darren
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.