I have my own custom code to query data from a form plugin and display it on the frontend. However, I cannot get the program to access the database. The connection is essentially just entering in the user, pass, and hostname so I am at a loss as to why it will not connect. Does anyone have any insights?
Here is the code I'm trying to get to work:
<?php
$dbuser="user";
$dbpass="password";
$host="localhost";
$dbname = "namegoeshere";
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
echo "Connected to database server<br>";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " . $database . " is selected";
mysql_close($chandle);
$leadId = 3;
$query = "SELECT field_number FROM wp_rg_lead_detail WHERE lead_id = '" . $leadId . "'";
$result = mysql_query($query, $sqlLink);
print("Query: $query");
$count = mysql_num_rows($result);
print("Ct: $count");
?>
idt answers:
You have
mysql_close($chandle);
before
$result = mysql_query($query, $sqlLink);
Try moving
mysql_close($chandle);
after
$result = mysql_query($query, $sqlLink);
Ashley comments:
Thanks, but still same issue even when that is corrected..
idt comments:
What is the exact error you get?
Change
$result = mysql_query($query, $sqlLink);
to
$result = mysql_query($query);
Ashley comments:
When the script is run, the error is:
Connected to database server
databse name here Database not found.databasenamehere
idt comments:
Based from your error message you have not specified the correct values for these variables:
$dbuser="user";
$dbpass="password";
$host="localhost";
$dbname = "namegoeshere";
$dbname should be the name of your database
$dbuser is the username that has access to your db
$dbpass is the password
Pippin Williamson answers:
Are you trying to access a table inside of the WordPress database, or another database all together?
Ashley comments:
Here is the code I'm trying to get to work:
<?php
$dbuser="user";
$dbpass="password";
$host="localhost";
$dbname = "namegoeshere";
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
echo "Connected to database server<br>";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " . $database . " is selected";
mysql_close($chandle);
$leadId = 3;
$query = "SELECT field_number FROM wp_rg_lead_detail WHERE lead_id = '" . $leadId . "'";
$result = mysql_query($query, $sqlLink);
print("Query: $query");
$count = mysql_num_rows($result);
print("Ct: $count");
?>
Ashley comments:
Here is the code I'm trying to get to work:
<?php
$dbuser="user";
$dbpass="password";
$host="localhost";
$dbname = "namegoeshere";
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
echo "Connected to database server<br>";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " . $database . " is selected";
mysql_close($chandle);
$leadId = 3;
$query = "SELECT field_number FROM wp_rg_lead_detail WHERE lead_id = '" . $leadId . "'";
$result = mysql_query($query, $sqlLink);
print("Query: $query");
$count = mysql_num_rows($result);
print("Ct: $count");
?>
Pippin Williamson comments:
I'm not sure why the database isn't connecting, but why don'y you just put the database you're trying to use inside of the WP database. That way you wouldn't have to worry about connecting to it.
If this is acceptable, I'll give you the appropriate code / resources.
Ashley comments:
I don't care how it works as long as it does!! Whatever it takes is fine with me.
Pippin Williamson comments:
ok, to create a db table within the WP db, do something like this (adjust values for your names and such):
function ss_options_install() {
global $wpdb;
// create the slide show database
if($wpdb->get_var("show tables like YOURTABLENAME") != YOURTABLENAME)
{
// create a table with two colums: fieldnumber and ID
$sql = "CREATE TABLE YOURTABLENAME (
id mediumint(9) NOT NULL AUTO_INCREMENT,
fieldnumber tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$rows_affected = $wpdb->insert( YOURTABLENAME, array( 'fieldnumber' => 'default') );
}
}
That will create your database. Now, to get info from the database:
<?php foreach ( $wpdb->get_results("SELECT * FROM YOURTABLENAME ORDER BY id DESC LIMIT 1;") as $key => $object)
{
$id = $object->id; // ID of each entry
$fieldnumber = $object->fieldnumber; // field number of each entry
// do stuff here
}?>
This code will retrieve every entry from YOURTABLENAME and set up variables to the two columns inside of the table: fieldnumber and ID.
All of this goes in functions.php or in your main plugin file.
Maor Barazany answers:
Paste here your connection code please, so it will be easier to see if you're doing something wrong.
Also - which error message do you get. and where do you put this code you write?
(Of course don't write the actual username/password/host/database..)
Ashley comments:
Here is the code I'm trying to get to work:
<?php
$dbuser="user";
$dbpass="password";
$host="localhost";
$dbname = "namegoeshere";
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
echo "Connected to database server<br>";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " . $database . " is selected";
mysql_close($chandle);
$leadId = 3;
$query = "SELECT field_number FROM wp_rg_lead_detail WHERE lead_id = '" . $leadId . "'";
$result = mysql_query($query, $sqlLink);
print("Query: $query");
$count = mysql_num_rows($result);
print("Ct: $count");
?>
Arief Fajar Nursyamsu answers:
If you are trying to access tables in the same database (your wordpress database) then you do not need to open a new database connection again.
You can run query or get the result of your query using wpdb class. Here is a sample of code to access your tables based on my latest project.
$latestproduct = $wpdb->get_results( 'SELECT * FROM product ORDER BY product.id DESC');
note: product is a table in the same database with wordpress
Hope this solve your problem.