I'm trying to create scheduled auto-updater for MySQL DB. The script I use is shown below. It's idea is to insert only unique products from table wp_posts to table export_copy. But this one inserts nothing.
Please help me to fix the script.
INSERT INTO `export_copy`(`name`, `link`)
SELECT p.post_title, p.guid
FROM wp_posts p
WHERE NOT EXISTS (
SELECT *
FROM export_copy, wp_posts
WHERE export_copy.id=wp_posts.ID)
Your query will never work!
Since your table's name is "wp_posts" ,it seem you want copy data from wordpress table to your own
you can handle this by php, firt you must fetch data from table and save that to an array, then export array to insert query
$link = mysqli_connect(Your_HOST , Your_User , YOUR_Pass, DB_NAME);
$q = "SELECT p.post_title, p.guid FROM wp_posts p WHERE NOT EXISTS ( SELECT * FROM export_copy, wp_posts WHERE export_copy.id=wp_posts.ID)";
$result = mysqli_query($link, $q);
$i=0;
while($row=mysqli_fetch_assoc($result))
{
$a[$i]['name'] = $row['post_title'];
$a[$i]['link'] = $row['post_title'];
$i++;
}
$q = '';
foreach($a as $a)
$q .= "('".$a['name']."','".$a['link']."'),";
$q = substr($q,0, (strlen($q)-1));
$q = "INSERT INTO `export_copy`(`name`, `link`) VALUES " . $q;
$result = mysqli_query($link, $q);
Related
Iam trying to edit table details.my edit link is EDIT
and im create a new page.(edit.php)to view edit details. id display($id=$_GET['id']; echo $id; in this page but that id's corresponding vales are not display.
mysql query is : $mylink = $wpdb->get_row( "SELECT * FROM $wpdb->test WHERE id = '$id'", ARRAY_A );
if iam trying to echo $mylink->test_name; ("test_name" is my table field name)
nothing to display(empty)
`
Try this for the same
$mylinks = $wpdb->get_results( "SELECT * FROM mytable WHERE id = '$id'" );
foreach( $mylinks as $mylink ) {
echo $mylink->test_name;
}
I'm using a SELECT * FROM "" WHERE "" = "" query and I'm not sure what I'm doing wrong with it. I'm trying to select an item based on its PO which is a completely unique identifier to one row in the table. Here's my process in doing so:
$jobnumber = $_GET['jref'];
$query = "SELECT * FROM `po_10152796` WHERE `po` = " .$jobnumber;
$results = mysqli_query($conn,$query) or die(mysqli_error($conn));
$rowitem = mysqli_fetch_array($results);
$jobname = $rowitem['Job Name'];
$phone = $rowitem['phone'];
Things i know are correct:
The "jobnumber" is retrieved correctly and matches up with an element in the table
The table is named "po_10152796" and there is a column named "po"
Forgot to post this, redid the code with prepared statements and it works, not sure what exactly I changed but here it is anyhow:
$jobnumber = $_GET['jref'];
$stmt = $conn->prepare( "SELECT `Job Name`, `Address`, `phone`, `description`, `materials` FROM po_10152796 WHERE po = ?");
$stmt->bind_param("i", $jobnumber);
if($stmt->execute()){
$stmt->bind_result($jobname, $address, $phone, $description, $materials);
$stmt->fetch();
}
try this line for the query
$query = "SELECT * FROM `po_10152796` WHERE `po` = '" .$jobnumber. "' ";
I am very confused about this (returning false):
$sql = "SELECT * from tbl_user WHERE group = 'abc'";
$res = mysql_query($sql);
if(mysql_num_rows($res) > 0) {
$response = array('status' => '1');
} else {
$response = array('status' => '0'); // ---> what I get back
die("Query failed");
}
...despite the fact the field group is present in mySQL database. Even more strange is that the following return the value of group:
$SQL = "SELECT * FROM tbl_user";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['group']; // ---> returns 'abc'
When I execute a WHERE clause with every other fields of my table excepting group (for example WHERE name = 'ex1' AND ID=1 AND isAllowed=0 (and so on...), everything is fine. As soon as I insert group = 'abc', I get nothing...
This makes me mad. If anyone could help... (I am running a local server with MAMP).
Thanks a lot!
The issue is that group is a reserved word in SQL.
For MySql you need to escape it with backticks
`group`
So your query would be
$sql = "SELECT * from tbl_user WHERE `group` = 'abc'";
This is what I'm doing now, how to do this without resorting to a subquery and without the php. I can just run it inside phpmyadmin directly:
<?php
$query = mysql_query(" SELECT node_id FROM embeds ");
while($row = mysql_fetch_assoc($query)) {
$node_id = $row['node_id'];
mysql_query(" INSERT INTO node_teaser(node_id, content) VALUES('$node_id', 'This is the teaser!') ");
}
?>
INSERT INTO node_teaser(node_id, content)
SELECT node_id, 'This is the teaser!' FROM embeds;
I am writing a plugin which grabs an array of data from the wordpress database using...
$data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);
This works fine and I can display all the info from the users table, the issue I have is that I need to also pull out the First name and Last name which are in the wp_usermeta table.
Is there a way to modify the statement to also pull those details from the other table?
Not in one query and not necessarily the slickest either, but the following will produce what you want:
$data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);
$i = 0;
foreach($data as $single) {
$meta = $wpdb->get_results(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = $single[ID]
AND (meta_key = 'first_name' OR meta_key = 'last_name')
ORDER BY meta_key",
ARRAY_A
);
$data[$i]['first_name'] = $meta[0]['meta_value'];
$data[$i]['last_name'] = $meta[1]['meta_value'];
$i++;
}
EDIT: Here it is in one query:
$data = $wpdb->get_results(
"SELECT $wpdb->users.*,
GROUP_CONCAT(
$wpdb->usermeta.meta_value
ORDER BY $wpdb->usermeta.meta_key
SEPARATOR ' '
) AS name
FROM $wpdb->users
LEFT JOIN $wpdb->usermeta
ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE ($wpdb->usermeta.meta_key = 'first_name'
OR $wpdb->usermeta.meta_key = 'last_name')
GROUP BY $wpdb->users.ID",
ARRAY_A
);
Note that opposed to the first version, the latter does not produce $data[x]['first_name'] and $data[x]['last_name'], but $data[x]['name'] instead. This is due to either being stored in the "meta_value" column. It is not possible to accomplish your task in one query and store the first and last name in two different array keys at the same time.
Hence, if doing it the second way, you'd have to use php's explode() function later on to access the name. Or correct it in a loop after the query has been run:
$i = 0;
foreach($data as $single) {
$name_parts = explode(' ', $single['name']);
$data[$i]['first_name'] = $name_parts[0];
$data[$i]['last_name'] = $name_parts[1];
$i++;
}