简体   繁体   English

如何使用MySQLI vs MySQL显示数据库中特定表行列的数据

[英]How to display data from specific table row columns in the database with MySQLI vs MySQL

I am still really new to MySQL and even newer to MySQLI. 我还是MySQL的新手,甚至还不熟悉MySQLI。 I am trying to update my website with MySQLI and I can't seem to fix this last issue. 我正在尝试使用MySQLI更新我的网站,但似乎无法修复最后一个问题。

My website is a single page site with 50 categories on the page. 我的网站是一个单页网站,页面上有50个类别。 Each category has a category title. 每个类别都有一个类别标题。 I have a table in the database with all 50 category titles and id's. 我在数据库中有一个表,其中包含所有50个类别标题和ID。 I was using MySQL/PHP to display each category title at the top of each category by choosing the row and column specifically. 我使用MySQL / PHP通过选择行和列来在每个类别的顶部显示每个类别的标题。 Here is my code.. 这是我的代码。

The Query: 查询:

$query_meta = mysql_query("SELECT * FROM `categories`", $connection);
    if (!$query_meta) {
        die("Connection Failed: " . mysql_error());
    }

$meta_query_result = array();

while( $row = mysql_fetch_assoc($query_meta) ) {
    $meta_query_result[$row['id']] = array('url' => $row['url'], 'name' => $row['name'], 'title' => $row['title']);
}

The Echo Statement: 回声声明:

<h2><?php echo $meta_query_result[1]['name']; ?></h2>   
<h6><?php echo $meta_query_result[1]['title']; ?></h6>

This allows me to choose the row and the column I want to display. 这使我可以选择要显示的行和列。

How can I redo this in MySQLI? 如何在MySQLI中重做此操作? Thanks in advance! 提前致谢!

you need to add the connection on the opposite side with mysqli. 您需要在与mysqli相反的一侧添加连接。

so // mysql_query($q, $db_connnect); 所以// mysql_query($q, $db_connnect); // becomes // mysqli_query($db_connnect, $q); //变成// // mysqli_query($db_connnect, $q);

Of course you should read documentation , because it looks the same (on first glance) as mysql but has little difference... 当然,您应该阅读文档 ,因为它看起来与mysql相同(乍看之下),但几乎没有区别...

Next example should be good for you: 下一个示例应该对您有利:

<?php

$db = new mysqli('localhost', 'root');
if ($db->connect_errno) {
    echo 'Connect failed: '.$db->connect_error;
    exit();
}
$db->select_db('test');
$cursor = $db->query('SELECT * FROM categories');
if ($db->error) {
    echo $db->error.PHP_EOL;
}
$meta_query_result = [];
while($row = $cursor->fetch_assoc()){
    $meta_query_result[$row['id']] = [
        'url' => $row['url'],
        'name' => $row['name'],
        'title' => $row['title'],
    ];
}
$cursor->close();

Notice:And remember that mysql_query is deprecated as of PHP 5.5.0, use PDO instead. 注意:请记住,从PHP 5.5.0开始不建议使用mysql_query,请改用PDO。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询数据库后如何从数组调用特定的行和列数据…(mysqli php) - How do i call specific row and column data from an array after querying a database… (mysqli php) PHP,HTML和MySqli-如何在一个表行中显示特定列的所有数据 - PHP, HTML and MySqli - how to show all data from specific column in one table row 如何使用php和mysqli将数据插入数据库中动态创建的表(html)行中 - how to insert data into a dynamically created row in a table (html) from database using php and mysqli 如何显示从mysqli数据库到HTML PHP表的Blob PDF数据 - How to Display Blob PDF data from mysqli database to your html PHP table 使用MySQLi从MySQL数据库删除ROW - Deleting a ROW from MySQL database using MySQLi 如何从数据库mysql获取总数据表并显示在android中 - How to get total data table from database mysql and display in android 如何将数据从 MySQL 数据库插入到表中显示到 Android? - How to Display Data from MySQL Database to Android by inserting it into Table? PHP:如何使用MySQLi在每个HTML表行中显示多个MySQL表记录 - PHP: How to display multiple MySQL table records per HTML table row using MySQLi 如何将数据从数组(PHP)保存到MySQL表? (使用MySQLi) - How to save data from array (PHP) to MySQL table? (using MySQLi) 使用数据库表中的特定数据行填充表单字段,以更新PHP MYSQL - Populate form field with specific data row from database table for updating PHP MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM